<?php
class Account extends Controller{
    public $data;
    function __construct(){
        parent::__construct();
        Session::init();
        $this->bar = new Bar();
        $this->model("model_website");
    }

    public function index(){
        if (Session::get("uid")){
            $data = "";
            //$data = $this->model_users->getUsers(1);
            //variable
            //$fb_token = $data[0]['fb_token'];
            //Check access token exists all platform

            //Facebook
            //$fb_url = "https://graph.facebook.com/me?access_token=". $fb_token;
            //$fb_url = "https://graph.facebook.com/me?access_token=CAAX2UktdvAUBAKEYPuSg6TZCUcWxIa0ZBl0kV8mQGXbOXgaSwXPKsZBVqI8GvGU5cDTz2GnHvqPcLLpRGnl5zNcqoCpmHZBPjxo9bDZB2zZAJm64zvatMhlQqzvYVf9ZBkgA9dLGNtCLr7WhZAK17wuUzVBLpSZBF1XuRW5RnDc9YKle7VhnFHbc30yGurwwUIud1pDrhlJ7mrIypq8oM3d37a";
            /*if($this->httpRequest($fb_url)== 200){
                $this->data['conFacebook'] = true;
            }else{
                $this->data['conFacebook'] = false;
            }*/
            /*$this->data['conFacebook'] = false;
            $this->data['conTwitter'] = false;
            $this->data['conZing'] = false;
            $this->data['conGoogle'] = false;
            $this->data['conLinked'] = false;*/

            //google

            //twitter

            //zing

            //linked

            //Bar content
            $bar_content = $this->bar->getBarAccount("account");
            $this->data['bar_content'] = $bar_content;

            //User
            if (Session::get("uid")){
                $user = $this->model_website->getUsersById(Session::get("uid"));
                $this->data['user'] = $user;
            }

            $this->data['view'] = "account";
            $this->data['category'] = "account";
            /*$this->data['type'] = "account";*/
            $this->data['title'] = "Tài khoản của tôi";
            $this->data['data'] = $data;
            $this->view->render("layout/body", $this->data);
        }else{
            header('location:' . ROOT_URL."login");
        }

    }

    public function getCampaign(){

        $uid = Session::get("uid");
        //Các chiến dịch user đang tham gia
        $campaignInProgress = $this->model_website->getCampaignInProgressUser($uid);
        //Các chiến dịch user được mời tham gia
        $campaignInvited = $this->model_website->getCampaignInvited($uid);
        //Các chiến dịch đang diễn ra
        $data = $this->model_website->getCampaignInProgress();
        $this->data['data'] = $data;
        $this->data['progress'] = $campaignInProgress;
        $this->data['invited'] = $campaignInvited;
        $this->view->render("account/account_campaign", $this->data);
    }

    public function getSurvey(){

        $uid = Session::get("uid");
        $user_survey = $this->model_website->getUserSurveyByUid($uid);
        $this->data['user_survey'] = $user_survey;

        //Survey thông tin cá nhân
        $surveyInformation = $this->model_website->getSurveyChildBySid(1);
        //Count surveyed today
        $surveyInformationCount = array();
        foreach($surveyInformation as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyInformationCount, $Count[0]);
        }

        $this->data['s_information'] = $surveyInformation;
        $this->data['s_information_count'] = $surveyInformationCount;

        //Survey Làm đẹp
        $surveyBeautiful = $this->model_website->getSurveyChildBySid(2);
        //Count surveyed today
        $surveyBeautifulCount = array();
        foreach($surveyBeautiful as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyBeautifulCount, $Count[0]);
        }

        $this->data['s_beautiful'] = $surveyBeautiful;
        $this->data['s_beautiful_count'] = $surveyBeautifulCount;

        //Survey hộ gia đình
        $surveyFamily = $this->model_website->getSurveyChildBySid(3);
        //Count surveyed today
        $surveyFamilyCount = array();
        foreach($surveyFamily as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyFamilyCount, $Count[0]);
        }

        $this->data['s_family'] = $surveyFamily;
        $this->data['s_family_count'] = $surveyFamilyCount;

        //Survey sức khỏe và thể thao
        $surveySport = $this->model_website->getSurveyChildBySid(4);
        //Count surveyed today
        $surveySportCount = array();
        foreach($surveySport as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveySportCount, $Count[0]);
        }

        $this->data['s_sport'] = $surveySport;
        $this->data['s_sport_count'] = $surveySportCount;

        //Survey công việc
        $surveyWork = $this->model_website->getSurveyChildBySid(5);
        //Count surveyed today
        $surveyWorkCount = array();
        foreach($surveyWork as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyWorkCount, $Count[0]);
        }

        $this->data['s_work'] = $surveyWork;
        $this->data['s_work_count'] = $surveyWorkCount;

        //Survey mua sắm
        $surveySales = $this->model_website->getSurveyChildBySid(6);
        //Count surveyed today
        $surveySalesCount = array();
        foreach($surveySales as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveySalesCount, $Count[0]);
        }

        $this->data['s_sales'] = $surveySales;
        $this->data['s_sales_count'] = $surveySalesCount;

        //Survey công nghệ
        $surveyTech = $this->model_website->getSurveyChildBySid(7);
        //Count surveyed today
        $surveyTechCount = array();
        foreach($surveyTech as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyTechCount, $Count[0]);
        }

        $this->data['s_tech'] = $surveyTech;
        $this->data['s_tech_count'] = $surveyTechCount;

        //Survey thức ăn và đồ uống
        $surveyEat = $this->model_website->getSurveyChildBySid(8);
        //Count surveyed today
        $surveyEatCount = array();
        foreach($surveyEat as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyEatCount, $Count[0]);
        }

        $this->data['s_eat'] = $surveyEat;
        $this->data['s_eat_count'] = $surveyEatCount;

        //Survey giải trí
        $surveyEntertainment = $this->model_website->getSurveyChildBySid(9);
        //Count surveyed today
        $surveyEntertainmentCount = array();
        foreach($surveyEntertainment as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyEntertainmentCount, $Count[0]);
        }
        $this->data['s_entertainment'] = $surveyEntertainment;
        $this->data['s_entertainment_count'] = $surveyEntertainmentCount;

        //Survey sở thích
        $surveyInterest = $this->model_website->getSurveyChildBySid(10);
        //Count surveyed today
        $surveyInterestCount = array();
        foreach($surveyInterest as $v){
            $Count = $this->model_website->getSurveyCountByScid($v['scid']);
            array_push($surveyInterestCount, $Count[0]);
        }
        $this->data['s_interest'] = $surveyInterest;
        $this->data['s_interest_count'] = $surveyInterestCount;

        $this->view->render("account/account_survey", $this->data);
    }

    public function getSurveySingle(){
        if(isset($_POST['sid'])){
            $uid = Session::get("uid");
            $sid = $_POST['sid'];
            $scid = $_POST['scid'];

            $user_survey = $this->model_website->getUserSurveyByScidAndUid($uid, $scid);
            if($user_survey){
                $this->data['surveyed'] = "true";
                //Lấy dữ liệu khảo sát của user
                $usid = $user_survey[0]['usid'];
                $user_answer = $this->model_website->getAnsweredByUsid($usid);
                if($user_answer){
                    for($i = 0; $i < sizeof($user_answer); $i++){
                        $user_answer_child = $this->model_website->getAnsweredChildren($user_answer[$i]['qid']);
                        $user_answer[$i]['children'] = $user_answer_child;
                    }
                }
                $this->data['user_survey'] = $user_answer;
            }else{
                $this->data['surveyed'] = "false";
            }

            $survey_child = $this->model_website->getSurveyChildById($scid);
            if($survey_child){
                $sid = $survey_child[0]['sid'];
            }
            //variable
            $parent_id = 0;
            $survey_child_title = "";
            foreach($survey_child as $value){
                $parent_id = $value['sid'];
                $survey_child_title = $value['survey_child_name'];
            }

            //get Survey folder title
            $survey = $this->model_website->getSurveyById($parent_id);
            $survey_title = $survey[0]['survey_name'];

            //get question from survey
            $question = $this->model_website->getQuestionParentBySid($scid);
            for($i = 0; $i < sizeof($question); $i++){
                $parent = $question[$i]['qid'];

                $type = $question[$i]['question_type'];
                if($type == 3){
                    $row = $this->model_website->getQuestionByParentId($parent);
                    $column = $this->model_website->getQuestionChildByQid($parent);
                    $question[$i]['row'] = $row;
                    $question[$i]['column'] = $column;
                }else{
                    $children = $this->model_website->getQuestionByParentId($parent);
                    $question[$i]['children'] = $children;
                }

            }
            $this->data['scid'] = $scid;
            //icon
            switch($sid){
                case "1":{
                    $this->data['icon_survey'] = "survey-profile-icon";
                    break;
                }
                case "2":{
                    $this->data['icon_survey'] = "survey-beautiful-icon";
                    break;
                }
                case "3":{
                    $this->data['icon_survey'] = "survey-family-icon";
                    break;
                }
                case "4":{
                    $this->data['icon_survey'] = "survey-sport-icon";
                    break;
                }
                case "5":{
                    $this->data['icon_survey'] = "survey-work-icon";
                    break;
                }
                case "6":{
                    $this->data['icon_survey'] = "survey-shop-icon";
                    break;
                }
                case "7":{
                    $this->data['icon_survey'] = "survey-technology-icon";
                    break;
                }
                case "8":{
                    $this->data['icon_survey'] = "survey-eat-icon";
                    break;
                }
                case "9":{
                    $this->data['icon_survey'] = "survey-entertainment-icon";
                    break;
                }
                case "10":{
                    $this->data['icon_survey'] = "survey-music-icon";
                    break;
                }
                default:{
                $this->data['icon_survey'] = "survey-profile-icon";
                break;
                }
            }

            $this->data['survey_child_title'] = $survey_child_title;
            $this->data['survey'] = $survey;
            $this->data['survey_title'] = $survey_title;
            $this->data['question'] = $question;
            $this->view->render("account/account_survey_single", $this->data);
        }
    }

    public function surveyed(){
        if(isset($_POST['scid'])){
            $scid = $_POST['scid'];
            $qid = $_POST['qid'];
            $type  = $_POST['type'];
            $uid = Session::get("uid");
            $usid = 0;
            //Kiểm tra user đã khảo sát survey chưa.
            $user_survey = $this->model_website->getUserSurveyByScidAndUid($uid, $scid);
            if($user_survey){
                $usid = $user_survey[0]['usid'];
            }else{
                $today = date("Y-m-d H:i:s");
                $usid = $this->model_website->addUserSurvey($uid, $scid, 0, 0, $today);
            }
            if($usid > 0){
                switch($type){
                    case "1":{
                        $asid = $_POST['asid'];
                        //Kiểm tra question đã được khảo sát chưa?
                        $answered = $this->model_website->getAnsweredByUsidAndQid($usid, $qid);
                        $today = date("Y-m-d H:i:s");
                        if($answered){
                            //Update answer
                            $this->model_website->updateAnswered($answered[0]['ansid'], $asid, "", $today);
                        }else{
                            $this->model_website->addAnswered($usid, $qid, 0, $asid, "", $today);
                        }
                        break;
                    }
                    case "2":{
                        $ansArr = $_POST['ansArr'];
                        if($ansArr){
                            //Kiểm tra question đã được khảo sát chưa?
                            $answered = $this->model_website->getAnsweredByUsidAndQid($usid, $qid);
                            $today = date("Y-m-d H:i:s");
                            if($answered){
                                //Update answer
                                $this->model_website->removeAnsweredByQuestionParent($qid);
                                for($i = 0; $i < sizeof($ansArr); $i++){
                                    $this->model_website->addAnswered(0, 0, $qid, $ansArr[$i], "", $today);
                                }
                            }else{
                                $this->model_website->addAnswered($usid, $qid, 0, "", "", $today);
                                for($i = 0; $i < sizeof($ansArr); $i++){
                                    $this->model_website->addAnswered(0, 0, $qid, $ansArr[$i], "", $today);
                                }
                            }
                        }
                        break;
                    }
                    case "3":{
                        $ansArrTable = $_POST['ansArr'];
                        if($ansArrTable){
                            //Kiểm tra question đã được khảo sát chưa?
                            $answered = $this->model_website->getAnsweredByUsidAndQid($usid, $qid);
                            $today = date("Y-m-d H:i:s");
                            if($answered){
                                //Update answer
                                $this->model_website->removeAnsweredByQuestionParent($qid);
                                for($j = 0; $j < sizeof($ansArrTable); $j++){
                                    $rowValue = $ansArrTable[$j][0];
                                    $columnValue = $ansArrTable[$j][1];
                                    $this->model_website->addAnswered(0, $rowValue, $qid, $columnValue, "", $today);
                                }
                            }else{
                                //Add new answer
                                $this->model_website->addAnswered($usid, $qid, 0, "", "", $today);
                                for($j = 0; $j < sizeof($ansArrTable); $j++){
                                    $rowValue = $ansArrTable[$j][0];
                                    $columnValue = $ansArrTable[$j][1];
                                    $this->model_website->addAnswered(0, $rowValue, $qid, $columnValue, "", $today);
                                }
                            }
                        }
                        break;
                    }
                    default :{
                    break;
                    }
                }
            }
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    //Information
    public function getInformation(){
        if(Session::get("uid")){
            $uid = Session::get("uid");
            $data = $this->model_website->getUserById($uid);
            if($data){

                $this->data['uid'] = $uid;
                $this->data['username'] = $data[0]['username'];
                $this->data['first_name'] = $data[0]['first_name'];
                $this->data['last_name'] = $data[0]['last_name'];
                $this->data['email'] = $data[0]['email'];
                $this->data['phone'] = $data[0]['phone'];
                $this->data['company'] = $data[0]['company'];
                $this->data['address_1'] = $data[0]['address_1'];
                $this->data['address_2'] = $data[0]['address_2'];
                $pid = $this->model_website->getProvinceById($data[0]['pid']);
                if($pid){
                    $this->data['province_name'] = $pid[0]['province'];
                    $this->data['pid'] = $pid[0]['pid'];
                }
                $province = $this->model_website->getProvince();
                $this->data['province'] = $province;
                //get children
                $children = $this->model_website->getChildrenByUid($uid);
                $this->data['children'] = $children;
            }

            $this->view->render("account/account_information", $this->data);
        }
    }

    public function saveAccount(){
        if(isset($_POST['uid'])){
            $uid = $_POST['uid'];
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $user = $this->model_website->getUsersById($uid);
            if($user){
                $usernameData = $user[0]['username'];
                if($usernameData != ""){
                    $this->model_website->updateUsernamePassword($uid, $username, $password);
                }else{
                    $this->model_website->updateUserPassword($uid, $password);
                }
                echo json_encode(array("status"=>"ok"));
            }else{
                echo json_encode(array("status"=>"error"));
            }
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    public function saveInformation(){
        if(isset($_POST['uid'])){
            $uid = $_POST['uid'];
            $first_name = $_POST['first_name'];
            $last_name = $_POST['last_name'];
            $gender = $_POST['gender'];
            $about = $_POST['about'];

            $this->model_website->updateAccountInformation($uid, $first_name, $last_name, $gender, $about);
            echo json_encode(array("status"=>"ok"));
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    public function saveContact(){
        if(isset($_POST['uid'])){
            $uid = $_POST['uid'];
            $email = $_POST['email'];
            $phone = $_POST['phone'];

            $this->model_website->updateAccountContact($uid, $email, $phone);
            echo json_encode(array("status"=>"ok"));
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    public function saveAddress(){
        if(isset($_POST['uid'])){
            $uid = $_POST['uid'];
            $company = $_POST['company'];
            $address_1 = $_POST['address_1'];
            $address_2 = $_POST['address_2'];
            $province = $_POST['province'];

            $this->model_website->updateAccountAddress($uid, $company, $address_1, $address_2, $province);
            echo json_encode(array("status"=>"ok"));
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    public function saveChildren(){
        if(isset($_POST[''])){

        }else{

        }
    }

    //Connect
    public function getConnect(){
        $uid = Session::get("uid");
        $data = $this->model_website->getUsersById($uid);
        $this->data['fb_id'] = $data[0]['fb_id'];
        $this->data['fb_token'] = $data[0]['fb_token'];
        $this->data['fb_user'] = $data[0]['fb_user'];
        $this->data['google_id'] = $data[0]['google_id'];
        $this->data['google_token'] = $data[0]['google_token'];
        $this->data['google_user'] = $data[0]['google_user'];
        $this->data['twitter_id'] = $data[0]['twitter_id'];
        $this->data['twitter_token'] = $data[0]['twitter_token'];
        $this->data['twitter_user'] = $data[0]['twitter_user'];
        $this->data['twitter_picture'] = $data[0]['twitter_picture'];
        $this->data['linkedin_id'] = $data[0]['linkedin_id'];
        $this->data['linkedin_token'] = $data[0]['linkedin_token'];
        $this->data['linkedin_user'] = $data[0]['linkedin_user'];

        $this->view->render("account/account_connect", $this->data);
    }

    //Kiểm tra access token có còn kết nối
    public function conFacebook(){
        $fb_token = "";
        $request = "https://graph.facebook.com/me?access_token=CAAX2UktdvAUBAEfiqEKORaiHZCQC8wRYIFRZBWJcKHgtlMgerYZCZAJS3U6fWZCXKz4P09XRPQ1vK5qTRiIU5ff063d0NAYqLRMRFezxIO7LLDtJtGy8ZBfxD1ZC2sO95SmdIrFH44E8iYBPfhCqSVrzaeFYgA83EjzcT7S34dlDsxlc4z0dkOD8wHdzXF5EZByTZCy9vt6PiVC9UzZAZC4A6Hz";
        if($this->httpRequest($request) == 200){
            echo json_encode(array("status"=>"ok"));
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    public function conGoogle(){
        $google_token = "ya29.cADuihB8KPb-7iAAAAAZECEpSb_p_-9P3lEII2N6nPcmdM9__cR63JpcEMQyNQ";
        require_once ROOT_PATH . '/views/platform/google/src/Google_Client.php';
        require_once ROOT_PATH . '/views/platform/google/src/contrib/Google_Oauth2Service.php';

        $client = new Google_Client();
        $client->setApplicationName("Buzzy");
        $client->setClientId('284763762002-ub31s924n1th0vvf3cpu8ckk75hubbe5.apps.googleusercontent.com');
        $client->setClientSecret('a8rIIQRWvI3KNHRjsZKkyUfy');
        $client->setRedirectUri(ROOT_URL.'account/google');

        $plus = new Google_Oauth2Service($client);

        $_SESSION['access_token'] = $google_token;

        if (isset($_SESSION['access_token'])) {
            var_dump("a");
            $client->setAccessToken(json_decode($_SESSION['access_token']));
        }
        var_dump($client->getAccessToken());
        if ($client->getAccessToken())
        {
            $userinfo = $plus->userinfo;
            var_dump($userinfo->get());

        }
    }

    public function facebook(){
        //facebook api
        $config = array();
        require ROOT_PATH . '/views/platform/facebook/src/config.php';
        require ROOT_PATH . '/views/platform/facebook/src/facebook.php';
        $facebook = new Facebook(array(
            'appId' => $config['App_ID'],
            'secret' => $config['App_Secret'],
            'cookie' => true
        ));

        if (isset($_GET['fbTrue'])) {
            $token_url = "https://graph.facebook.com/oauth/access_token?"
                . "client_id=" . $config['App_ID'] . "&redirect_uri=" . urlencode($config['callback_url'])
                . "&client_secret=" . $config['App_Secret'] . "&code=" . $_GET['code'];

            $response = file_get_contents($token_url);
            $params = null;
            parse_str($response, $params);

            $graph_url = "https://graph.facebook.com/me?access_token=" . $params['access_token'];
            $fb_user = json_decode(file_get_contents($graph_url, true));

            $fb_id = $fb_user->id;
            $fb_username = $fb_user->username;
            $fb_token = $params['access_token'];

            if(Session::get("uid")){
                $uid = Session::get("uid");
                $this->model_website->updateUserFacebook($uid, $fb_id, $fb_username, $fb_token);
            }

            echo "<script>window.close();</script>";
        }

    }

    public function twitter(){
        require_once(ROOT_PATH . '/views/platform/twitter/oauth/twitteroauth.php');
        require_once(ROOT_PATH . '/views/platform/twitter/config.php');
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
        $twitterUpdate = false;
        if (isset($_GET['oauth_token'])) {
            $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

            $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
            $_SESSION['access_token'] = $access_token;

            $content = $connection->get('account/verify_credentials');

            $twitter_id = $content->id;
            $twitter_name = $content->screen_name;
            $twitter_token = $access_token['oauth_token'];
            $twitter_image = $content->profile_image_url;
            if(Session::get("uid")){
                $uid = Session::get("uid");
                $this->model_website->updateUserTwitter($uid, $twitter_id, $twitter_name, $twitter_token, $twitter_image);
            }
            echo "<script>window.close();</script>";
        }
    }

    public function google(){
        require_once ROOT_PATH . '/views/platform/google/src/Google_Client.php';
        require_once ROOT_PATH . '/views/platform/google/src/contrib/Google_Oauth2Service.php';

        $client = new Google_Client();
        $client->setApplicationName("Buzzy");

        $oauth2 = new Google_Oauth2Service($client);

        $authUrl = $client->createAuthUrl();

        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $google_token = $client->getAccessToken();
            $_SESSION['token'] = $google_token;
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            $user_google = $oauth2->userinfo->get();

            var_dump($user_google);
            $google_id = $user_google['id'];
            $google_name = $user_google['name'];

            /*$google_picture = $user_google['picture'];*/
        }

        if (isset($_SESSION['token'])) {
            $client->setAccessToken($_SESSION['token']);
        }

        if (isset($_REQUEST['logout'])) {
            unset($_SESSION['token']);
            $client->revokeToken();
        }

        //echo "<script>window.close();</script>";
    }



    public function linked(){
        $config = array();
        require_once ROOT_PATH . '/views/platform/linkedin/oAuth/config.php';
        require_once ROOT_PATH . '/views/platform/linkedin/oAuth/linkedinoAuth.php';
        require_once ROOT_PATH . '/views/platform/linkedin/oAuth/class.linkedClass.php';
        $linkedClass   =   new linkedClass();
        $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret']);

        if (isset($_REQUEST['oauth_verifier'])){
            $_SESSION['oauth_verifier']     = $_REQUEST['oauth_verifier'];

            $linkedin->request_token    =   unserialize($_SESSION['requestToken']);
            $linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
            $linkedin->getAccessToken($_REQUEST['oauth_verifier']);
            $_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
        }
        else{
            $linkedin->request_token    =   unserialize($_SESSION['requestToken']);
            $linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
            $linkedin->access_token     =   unserialize($_SESSION['oauth_access_token']);
        }
        $content1 = $linkedClass->linkedinGetUserInfo($_SESSION['requestToken'], $_SESSION['oauth_verifier'], $_SESSION['oauth_access_token']);


        $xml   = simplexml_load_string($content1);
        $array = $this->XML2Array($xml);
        $content = array($xml->getName() => $array);

        var_dump($content);

    }

    //Lấy đường dẫn truy cập của các nền tảng
    public function facebookUrl(){
        $config = array();
        require_once ROOT_PATH . '/views/platform/facebook/src/config.php';
        require_once ROOT_PATH . '/views/platform/facebook/src/facebook.php';
        $facebook = new Facebook(array(
            'appId' => $config['App_ID'],
            'secret' => $config['App_Secret'],
            'cookie' => true
        ));
        $url = 'https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,public_profile,publish_stream';

        echo json_encode(array("status"=>"ok", "url"=>$url));
    }

    public function googleUrl(){
        require_once ROOT_PATH . '/views/platform/google/src/Google_Client.php';
        require_once ROOT_PATH . '/views/platform/google/src/contrib/Google_Oauth2Service.php';

        $client = new Google_Client();
        $client->setApplicationName("Buzzy");
        $oauth2 = new Google_Oauth2Service($client);
        $authUrl = $client->createAuthUrl();

        echo json_encode(array("status"=>"ok", "url"=>$authUrl));
    }

    public function twitterUrl(){
        require_once(ROOT_PATH . '/views/platform/twitter/oauth/twitteroauth.php');
        require_once(ROOT_PATH . '/views/platform/twitter/config.php');
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
        $request_token = $connection->getRequestToken(OAUTH_CALLBACK);

        $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
        $twitter_url = "";
        switch ($connection->http_code) {
            case 200:
                $twitter_url = $connection->getAuthorizeURL($token);
                break;
            default:
                echo 'Could not connect to Twitter. Refresh the page or try again later.';
        }

        echo json_encode(array("status"=>"ok", "url"=>$twitter_url));
    }

    public function linkedUrl(){
        $config = array();
        require_once ROOT_PATH . '/views/platform/linkedin/oAuth/config.php';
        require_once ROOT_PATH . '/views/platform/linkedin/oAuth/linkedinoAuth.php';
        $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url']);
        $linkedin->getRequestToken();
        $_SESSION['requestToken'] = serialize($linkedin->request_token);
        $url = $linkedin->generateAuthorizeUrl();

        echo json_encode(array("status"=>"ok", "url"=>$url));
    }


    //Buzzscore
    public function getBuzzscore(){
        $this->view->render("account/account_buzzscore", $this->data);
    }

    //History
    public function getHistory(){
        $this->view->render("account/account_history", $this->data);
    }




    //Connect https url
    function httpRequest($url) {
        $handle = curl_init($url);
        curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($handle);
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

        curl_close($handle);
        return $httpCode;
    }

    function XML2Array(SimpleXMLElement $parent)
    {
        $array = array();
        foreach ($parent as $name => $element) {
            ($node = & $array[$name])
            && (1 === count($node) ? $node = array($node) : 1)
            && $node = & $node[];
            $node = $element->count() ? XML2Array($element) : trim($element);
        }
        return $array;
    }

}
?>